﻿<?php
/**
* Deals Management
*/
class Admin_DealController extends Zend_Controller_Action{
		
	function init(){
		// System_Template::using('blank','Admin');
		System_Template::using('getit','Admin');	
		// get parameter in request URL
		$this->Params = $this->getRequest()->getParams();            
		$this->view->maintitle = "Deal Management";
		$this->model = new Admin_Model_Deal();
		parent::init();
	}
	
	function indexAction(){
		$data = $this->model->fetchAllWithStoreName();
		//echo "<pre>";
		//print_r($rs);
		//echo "</pre>";
		//$sD = config::getSourcesDescription();
		//$data = $sD->module->toArray();
		//$this->STOREMODEL = new Admin_Model_Store();
		$this->view->data = $data;
		//$this->view->dataSD = $sD;
		//$this->view->dataSources = $data;
		//$store = $this->STOREMODEL->findByStoreById()
	}
	
	/**
	 * @author uyen.do
	 * INVISIBLE
	 * Redirect to add or edit form
	 **/
	function inputAction(){
		$this->STOREMODEL = new Admin_Model_Store();
		$listStore = $this->STOREMODEL->findAll();
		$id = (isset($this->Params['id']) ? $this->Params['id'] : 0);
		/* Truong hop cap nhat*/
		if($id > 0){
			$data = $this->model->find($id)->current();
			$this->view->data = $data;
			$this->view->id = $id;
		}
		$this->view->listStore = $listStore;
	}
	
	/**
	 *
	 * @author uyen.do
	 * INVISIBLE
	 * Save a new User or edit an existed User
	 **/
	function saveAction(){
		if($this->Params['STATUS'] == 'on'){
			$this->Params['STATUS'] = '1';
		}else{
			$this->Params['STATUS'] = '0';
		}
		if($this->Params['DEALID'] == ''){
			$this->model->addNewDeal($this->Params);
		}else{
			$this->model->updateDeal($this->Params);
		}
		
		// Luu lai phan trang truoc
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/deal/index');
		}else{
			$this->_redirect('/admin/deal/index/limit'.$this->Params['limit'].'/page/'.$this->Params['page']);
		}
		
	}
	
	/**
	 * @author uyen.do
	 * change status of Deal
	 **/
	function changestatusAction(){
		$db = $this->model->getDefaultAdapter();
		$itemID = $this->Params['itemID'];
		$status = $this->Params['status'];
		$data = array('STATUS' => (int)$status);
		$rs = $db->update('deal',$data, ' DEALID = '. (int) $itemID);
		exit;
	}
	
	/**
	 *
	 * @author uyen.do
	 * change status of muti Deals
	 * 
	 **/
	
	function changestatusmultiitemsAction(){
		$ids = $this->Params['itemIDs'];
		$status = $this->Params['status'];
		$db = $this->model->getDefaultAdapter();
		foreach($ids as $id){
			$data = array('STATUS' => (int)$status);
			$rs = $db->update('deal',$data, 'DEALID = '.(int)$id);
		}
	}
	
	/**
	 *
	 * @author uyen.do
	 * delete a Deal
	 * 
	 **/
	
	function deleteAction(){
		$itemID = $this->Params['id'];
		// Logging class initalization
		$db = $this->model->getDefaultAdapter();
		$db->delete('deal', 'DEALID = '.(int) $itemID);
		// Luu lai phan trang truoc
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/deal/index');
		}else{
			$this->_redirect('/admin/deal/index/limit'.$this->Params['limit'].'/page/'.$this->Params['page']);
		}
		
	}
	
	/**
	 *
	 * @author uyen.do
	 * delete selected Deals
	 * 
	 **/
	
	function deletemultiitemAction(){
		$ids = $this->Params['ITEM'];
		$this->logger->info(sizeof($ids));
		$db = $this->model->getDefaultAdapter();
		
		foreach($ids as $id){
			$db->delete('deal', 'DEALID = '.(int)$id);
		}
		
		// Luu lai phan trang truoc
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/deal/index');
		}else{
			$this->_redirect('/admin/deal/index/limit'.$this->Params['limit'].'/page/'.$this->Params['page']);
		}
		
	}
	
	
}